package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.AccountDetail;
import com.chen.mapper.AccountDetailMapper;
import com.chen.service.AccountDetailService;
import com.chen.service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Service
public class AccountDetailServiceImpl extends ServiceImpl<AccountDetailMapper, AccountDetail> implements AccountDetailService{

    @Autowired
    private RecordsService<AccountDetail> recordsService;

    @Autowired
    private AccountDetailMapper accountDetailMapper;

    @Override
    public Page<AccountDetail> findPageForList(Page<AccountDetail> page, Long userId, String userName, String mobile, Long coinId, String amountStart, String amountEnd, Long accountId, String startTime, String endTime) {
        try {


            RecordsServiceImpl.SearchRecords searchRecords = new RecordsServiceImpl.SearchRecords();

            searchRecords.setCoinId(coinId);
            searchRecords.setUserId(userId);
            searchRecords.setUserName(userName);
            searchRecords.setMobile(mobile);
            searchRecords.setAccountId(accountId);

            Map<RecordsServiceImpl.SearchRecords.BetweenDataEnum, Map<String, RecordsServiceImpl.SearchRecords.LimitRecords>> betweenData = new HashMap<>();

            //设置金钱区间
            Map<String, RecordsServiceImpl.SearchRecords.LimitRecords> limitRecordsMap = new HashMap<>();
            limitRecordsMap.put("amount", RecordsServiceImpl.SearchRecords.LimitRecords.builder().min(amountStart).max(amountEnd).build());
            betweenData.put(RecordsServiceImpl.SearchRecords.BetweenDataEnum.AMOUNT, limitRecordsMap);

            //设置金钱区间
            limitRecordsMap = new HashMap<>();
            limitRecordsMap.put("created", RecordsServiceImpl.SearchRecords.LimitRecords.builder().min(startTime).max(endTime).build());
            betweenData.put(RecordsServiceImpl.SearchRecords.BetweenDataEnum.TIME, limitRecordsMap);


            searchRecords.setBetweenData(betweenData);

            return recordsService.findData(page,accountDetailMapper,searchRecords);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

}
